package com.web.dao.impl;

import com.entity.Condition;
import com.entity.Employrecord;
import com.entity.EmployrecordExample;
import com.mapper.EmployrecordMapper;
import com.page.PageInfo;
import com.web.dao.EmployrecordDao;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("employrecordDao")
public class EmployrecordDaoImpl implements EmployrecordDao {
    @Resource
    EmployrecordMapper employrecordMapper;

    public List<Employrecord> getAll() throws Exception {
        return employrecordMapper.getAll();
    }





    /**
     *
     * @param pageInfo
     * @return
     */
  public    List<Employrecord> getEmployrecordsByPage(PageInfo pageInfo) throws Exception {
      Map<String,Object> map = new HashMap<String,Object>();
      map.put("currPage",pageInfo.getCurrPage());
      map.put("pageSize",pageInfo.getPageSize());

      int itemCount = (int)employrecordMapper.countByExample(null);
      pageInfo.setItemCount(itemCount);
      return employrecordMapper.getEmployrecordsByPage(map);
  }


    public    List<Employrecord> searchEmployeesByPage(PageInfo pageInfo, List<Condition> conditions) throws Exception {
        int  currPage =  pageInfo.getCurrPage();
        int pageSize = pageInfo.getPageSize();
        int offset  = (currPage - 1) * pageSize;

        EmployrecordExample employrecordExample = new EmployrecordExample();
        employrecordExample.setOffset(offset);
        employrecordExample.setLimit(pageSize);

        EmployrecordExample.Criteria criteria = employrecordExample.createCriteria();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        for(Condition condition:conditions){
            String fdName = condition.getFdName();
            String fdVal= condition.getValue();
            switch(fdName){
                case "dids":
                    criteria.andDidIn(Arrays.asList(fdVal.split(",")));
                    break;
                case "msex":
                    break;
                case "mbirthday":
                    break;
                case "cid":
                    criteria.andCidEqualTo(fdVal);
                    break;
                case "edate":
                    switch (condition.getOp()){
                        case    "1":
                            criteria.andBegindateGreaterThanOrEqualTo(df.parse(condition.getValue()));
                        break;
                        case   "2":
                            criteria.andBegindateLessThanOrEqualTo(df.parse(condition.getValue()));
                        break;
                        case    "3":
                        criteria.andBegindateBetween(df.parse(condition.getValue()),df.parse(condition.getValue2()));
                        break;
                        default:break;
                    }

                    break;
                default: break;

            }
        }
        return employrecordMapper.selectByExampleWithDietitian(employrecordExample);
    }




    public int add(Employrecord employrecord) throws Exception {
        return employrecordMapper.insertSelective(employrecord);
    }

    public int update(Employrecord employrecord) throws Exception {
        return employrecordMapper.updateByPrimaryKeySelective(employrecord);
    }
}
